package cn.lingyangwl.agile.msg.push.api;

import cn.lingyangwl.agile.msg.push.api.constatnts.*;
import cn.lingyangwl.agile.msg.push.api.model.rqrs.*;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.core.response.RetOps;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
@ConditionalOnClass({FeignAutoConfiguration.class, NacosServiceAutoConfiguration.class})
public class MsgPushCloudApiImpl implements MsgPushApi {
    @Resource
    private MsgPushFeignApi msgPushFeignApi;

    @Override
    public void push(MsgPushParam req) {
        Resp<?> resp = msgPushFeignApi.push(req);
        RetOps.of(resp).assertSuccess((e) -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void checkNoAuthUserCaptcha(CheckNoAuthUserCaptchaReq req) {
        Resp<?> resp = msgPushFeignApi.checkNoAuthUserCaptcha(req);
        RetOps.of(resp).assertSuccess((e) -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void checkAuthUserCaptcha(CheckAuthUserCaptchaReq req) {
        Resp<?> resp = msgPushFeignApi.checkAuthUserCaptcha(req);
        RetOps.of(resp).assertSuccess((e) -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void sendNoAuthUserCaptcha(SendCaptchaMsgReq req) {
        Resp<?> resp = msgPushFeignApi.sendNoAuthUserCaptcha(req);
        RetOps.of(resp).assertSuccess((e) -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public List<String> listTemplateIdsByCodes(NoticeCodeEnum... noticeCodes) {
        List<String> list = Arrays.stream(noticeCodes).map(NoticeCodeEnum::getCode).collect(Collectors.toList());
        Resp<List<String>> resp = msgPushFeignApi.listTemplateIdsByCodes(list);
        RetOps.of(resp).assertSuccess((e) -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }
}
